﻿@using Newtouch.Infrastructure;
@{
    ViewBag.Title = "Form";
    Layout = "~/Views/Shared/_Form.cshtml";
    //病历规则校验开关
    var Bl_RuleOpen= SysConfigReader.String("Bl_RuleOpen");
}

<form id="form1">
    <div style="text-align: left; margin-bottom: 10px;height:25px;margin-left:5px;">
        <span class="glyphicon glyphicon-user" style="color:#07bbf1; font-size: 20px;width:20px;height:20px;border:2px solid #07bbf1;margin:5px 5px;"></span>
        <input id="brjs" name="brjs" type="text" value="病人简述" readonly="readonly" style="border:none;width:70%;font-size:15px;height:15px;padding:5px 2px;color:#07bbf1;" />
        <input id="zyh" name="zyh" type="hidden" class="form-control required" value="住院号" readonly="readonly" style="border:none;" />
        <input id="mzh" name="mzh" type="hidden" class="form-control required" value="门诊号" readonly="readonly" style="border:none;" />
    </div>
    <hr />
    <div style="width:20%;float:left;border:solid 1px #d0c9c9;border-radius:5px;margin-left:5px;">
        @Html.Partial("_BllxTree")
    </div>
    <div style="width:76%;float:right;margin:5px 10px;">
        <table class="form" style="padding-right:15px;">
            <tr>
                <th class="formTitle">病历类型</th>
                <td class="formValue">
                    <input id="bllxmc" name="bllxmc" type="text" class="form-control required" readonly="readonly" />
                    <input id="bllx" name="bllx" type="hidden" />
                </td>
                <th class="formTitle">病历时间</th>
                <td class="formValue" colspan="2">
                    <input id="blsj" type="text" class="form-control input-wdatepicker formClearIgnore" onclick="WdatePicker({ dateFmt: 'yyyy-MM-dd HH:mm:ss' })" onfocus="WdatePicker({ dateFmt: 'yyyy-MM-dd HH:mm:ss'})" value="@DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")" />
                </td>
            </tr>
            <tr>
                <th class="formTitle">模板权限</th>
                <td class="formValue">
                    <select id="mbqx" name="mbqx" class="form-control required" data-EnumType="Enummbqx"></select>
                </td>
                <th class="formTitle">关键字</th>
                <td class="formValue">
                    <input id="txt_keyword" name="txt_keyword" type="text" class="form-control" />
                </td>
                <td class="formValue">
                    <button id="btn_search" type="button" class="btn btn-primary"><i class="fa fa-search"></i></button>
                </td>
            </tr>
        </table>
        <div>
            <table id="mbgridList"></table>
        </div>
    </div>
</form>
<script>
    var keyValue = $.request("keyValue");
    var bllx = $.request("bllx");
    var zyh = $.request("zyh");
    var mzh = $.request("mzh");
    $(function () {
        if (!!zyh) {
            $.najax({
                url: "/MedicalRecordManage/PatInfo/MedRecordTreeEditJson",
                data: { keyValue: keyValue, bllx: bllx, zyh: zyh },
                dataType: "json",
                success: function (data) {
                    $("#form1").formSerialize(data);
                }
            });
        }
        else if (!!mzh) {
            $.najax({
                url: "/MedicalRecordManage/Outpatient/MedRecordTreeEditJson",
                data: { keyValue: keyValue, bllx: bllx, mzh: mzh },
                dataType: "json",
                success: function (data) {
                    $("#form1").formSerialize(data);
                }
            });
        }
        else {
            $('#zt').click(function () {
                return false;
            });
        }
        initControl();
    });

    function initControl() {
        var mzbz = '@((int)EnumMzbz.zy)';
        if (!!mzh) {
            mzbz = '@((int)EnumMzbz.mz)';
        }
        $('#mbqx').change(function () {
            mbListRefresh()
        });
        mbgridList();
        bllxTreeView("", bllx,mzbz);
        //if (!!bllx) {
        //    treeProcess(bllx);
        //}

        $("#btn_search").click(function () {
            mbListRefresh();
        });
    }

    //双击病历大类处理
    function treeProcess(id) {
        $.najax({
            url: "/SystemManage/Common/GetBllxbyId",
            data: { keyValue: id },
            dataType: "json",
            async: false,
            success: function (data) {
                $("#form1").formSerialize(data);
                mbListRefresh();
            }
        });
    }

    function AcceptClick(callBack) {
        if (!$('#form1').formValid()) {
            return false;
        }
        var mbRowId = $("#mbgridList").jqGrid('getGridParam', 'selrow');
        var mbData = $("#mbgridList").jqGrid('getRowData', mbRowId);
        if (mbRowId != null) {

            //入院病例、出院病例、首次病程、病案首页不能重复新建
            var isRepeat = false;
            var bllx = $("#bllx").val();
            if (bllx == "3032" || bllx == "3033" || bllx == "5") {

                $.ajax({
                    type: "POST",
                    url: "/MedicalRecordManage/PatInfo/getRepeatBl",
                    dataType: "json",
                    data: { zyh: zyh, bllx: bllx },
                    async: false,
                    success: function (ajaxresp) {
                        if (ajaxresp.length > 0) {
                            isRepeat = true;
                            $.modalAlert(mbData.bllxmc + "已存在!", 'warning');
                        }
                    }
                });
            }
            if (isRepeat == 'flase') {
                if ('@ViewBag.IsEnableEditor' == "true" && $("#bllx").val() == '@((int)EnumBllx.basy)') {
                    top.top.$.Newtouchtab.addTabWithOutMenu({ name: "病案首页", enName: "", url: "/MRHomePage/MRHomePage/Main?mbbh=" + mbRowId + "&keyValue=&zyh=" + $("#zyh").val(), AppId: "EMR" });
                }
                else if (mbData.LoadWay == '@((int)EnummbqxTempLoadWay.View)' && !!mbData.mblj) {
                    top.top.$.Newtouchtab.addTabWithOutMenu({ name: mbData.mbmc, enName: "", url: mbData.mblj + "?mbbh=" + mbRowId + "&keyValue=&zyh=" + $("#zyh").val(), AppId: "EMR" });
                }
                else {
                    top.top.$.Newtouchtab.addTabWithOutMenu({ name: "病历操作", enName: "", url: "/MedicalRecordManage/MedicalRecord/MedicalRecordEdiForAdd?mbbh=" + mbRowId + "&zyh=" + $("#zyh").val() + "&mzh=" + $("#mzh").val(), AppId: "EMR" });
                }
            }
            $.modalClose();
        }
        else {
            $.modalAlert("请选择病历模板", 'warning');
        }
    }

    function mbgridList() {
        //debugger
        var $gridList = $("#mbgridList");
        $gridList.dataGrid({
            url: "/MedicalRecordManage/PatInfo/GetMedRecordTempList",
            postData: { mbqx: $("#mbqx").val(), bllx: bllx },
            height: $(window).height()-200,
            colModel: [
                { label: "模板ID", name: "Id", hidden: true, key: true },
                { label: "模板加载方式", name: "LoadWay", hidden: true },
                { label: "模板路径", name: "mblj", hidden: true },
                { label: "bllx", name: "bllx", width: 30, hidden: true },
                {
                    label: '模板类型', name: 'bllxmc', hidden: true,align: 'center'
                },
                {
                    label: '模板名称', name: 'mbmc', width: 230, align: 'left'
                },
                {
                    label: "来源", name: "Memo", width: 30, align: 'left', formatter: function (val) {
                        if (val != "" && val != null) {
                            return "[转]";
                        }
                        else {
                            return "";
                        }
                    }
                },
                {
                    label: "模板性质", name: "Isempty", width: 60, align: "center", formatter: function (cellvalue) {
                        if (cellvalue == "1")
                            return "空模板";
                        else
                            return '定制模板';
                    }
                },
                { label: '创建日期', name: 'CreateTime', width: 100, align: 'center', formatter: "date", formatoptions: { srcformat: 'Y-m-d H:i:s', newformat: 'Y-m-d H:i:s' } },
                { label: '修改日期', name: 'LastModifierTime', width: 100, align: 'center', formatter: "date", formatoptions: { srcformat: 'Y-m-d H:i:s', newformat: 'Y-m-d H:i:s' } },

            ], onSelectRow: function (row) {
                var rowData = $('#mbgridList').jqGrid('getRowData', row);
                $("#bllxmc").val(rowData.bllxmc);
                $("#bllx").val(rowData.bllx);
            }
            , ondblClickRow: function (row) {
                var mbRowId = $("#mbgridList").jqGrid('getGridParam', 'selrow');
                var mbData = $("#mbgridList").jqGrid('getRowData', mbRowId);;
                debugger
                if (mbRowId != null) {
                    //入院病例、出院病例、首次病程、病案首页不能重复新建
                    var isRepeat = false;
                    var bllx = $("#bllx").val();
                    if (bllx == "3032" || bllx == "3033" || bllx == "5") {

                        $.ajax({
                            type: "POST",
                            url: "/MedicalRecordManage/PatInfo/getRepeatBl",
                            dataType: "json",
                            data: { zyh: zyh, bllx: bllx},
                            async: false,
                            success: function (ajaxresp) {
                                if (ajaxresp.length > 0) {
                                    isRepeat = true;
                                    $.modalAlert(mbData.bllxmc + "已存在!", 'warning');
                                }
                            }
                        });
                    }
                    if (isRepeat == false) {
                        var sxqx = "T";
                        var zksj = "";
                        var currpatinfo = "";
                        var applystatus = "";
                        debugger;
                        $.ajax({
                            type: "POST",
                            url: "/MedicalRecordManage/PatInfo/checkBlRules",
                            dataType: "json",
                            data: { zyh: zyh, bllx: bllx },
                            async: false,
                            success: function (ajaxresp) {
                                sxqx = ajaxresp.newblbznewblbz;
                                zksj = ajaxresp.zkrq;
                                currpatinfo = ajaxresp.zybrxx;
                                applystatus = ajaxresp.applystatus;
                            }
                        });
                        if (sxqx != "T" && '@Bl_RuleOpen' === 'ON') {
                            if (applystatus == "N") {
                                $.modalAlert(mbData.bllxmc + "的编写申请暂未审批通过，请联系质控办!", 'warning');
                                return;
                            }
                            $.modalConfirm(mbData.bllxmc + "未在规定时间内书写,请申请!",
                             function (flag) {
                                 if (flag) {
                                     $.modalOpen({
                                         id: "MedicalApplySend",
                                         title: "病历编写申请",
                                         url: "/MedicalRecordManage/PatInfo/MedicalApplySend?zyh=" + zyh + "&xm=" + currpatinfo.xm + "&ch=" + currpatinfo.BedName
                                         + "&sqblmc=" + mbData.mbmc + "&zkwcsj=" + zksj + "&bllx=" + bllx + "&Ryrq=" + currpatinfo.ryrq + "&Cyrq=" + currpatinfo.cqrq,
                                         width: "900px",
                                         height: "750px",
                                         callBack: function (iframeId, a) {
                                             top.frames[iframeId].submitForm();
                                         }
                                     });
                                 }
                             });
                        }
                        else {
                            if ('@ViewBag.IsEnableEditor' == "true" && $("#bllx").val() == '@((int)EnumBllx.basy)') {
                                top.top.$.Newtouchtab.addTabWithOutMenu({ name: "病案首页", enName: "", url: "/MRHomePage/MRHomePage/Main?mbbh=" + mbRowId + "&keyValue=&zyh=" + $("#zyh").val(), AppId: "EMR" });
                            }
                            else if (mbData.LoadWay == '@((int)EnummbqxTempLoadWay.View)' && !!mbData.mblj) {
                                top.top.$.Newtouchtab.addTabWithOutMenu({ name: mbData.mbmc, enName: "", url: mbData.mblj + "?mbbh=" + mbRowId + "&keyValue=&zyh=" + $("#zyh").val(), AppId: "EMR" });
                            }
                            else {
                                top.top.$.Newtouchtab.addTabWithOutMenu({ name: "病历操作", enName: "", url: "/MedicalRecordManage/MedicalRecord/MedicalRecordEdiForAdd?mbbh=" + mbRowId + "&zyh=" + $("#zyh").val() + "&mzh=" + $("#mzh").val(), AppId: "EMR" });
                            }
                            $.modalClose();
                        }
                    }
                    //$.modalClose();
                }

                else {
                    $.modalAlert("请选择病历模板", 'warning');
                }
        }
        });
    }

    function mbListRefresh() {
        if ($("#mbqx").val() != "" && $("#mbqx").val() != null && $("#mbqx").val() != "undefined") {
            $("#mbgridList").jqGrid('setGridParam', {
                postData: { mbqx: $("#mbqx").val(), bllx: $("#bllx").val(),keyword: $("#txt_keyword").val() }
            }).trigger('reloadGrid');
        }
    }
</script>